package com.atguigu.flink.chapter07.state;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.state.*;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.runtime.state.FunctionInitializationContext;
import org.apache.flink.runtime.state.FunctionSnapshotContext;
import org.apache.flink.streaming.api.checkpoint.CheckpointedFunction;
import org.apache.flink.streaming.api.datastream.BroadcastStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.BroadcastProcessFunction;
import org.apache.flink.util.Collector;

import java.util.ArrayList;

public class Flink02_State_BCState {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(2);

        MapStateDescriptor<String, String> controlStateDesc =
                new MapStateDescriptor<>("controlState", String.class, String.class);

        DataStreamSource<String> dataStream = env
                .socketTextStream("hadoop162", 8888);
        DataStreamSource<String> controlStream = env
                .socketTextStream("hadoop162", 9999);

        // 1. 把控制流做成广播流
        BroadcastStream<String> bcStream = controlStream.broadcast(controlStateDesc);

        // 2. 数据流进行connect
        dataStream
                .connect(bcStream)
                .process(new BroadcastProcessFunction<String, String, String>() {
                    // 处理数据流中的数据
                    @Override
                    public void processElement(String value, ReadOnlyContext ctx, Collector<String> out) throws Exception {
                        ReadOnlyBroadcastState<String, String> bcState = ctx.getBroadcastState(controlStateDesc);
                        String aSwitch = bcState.get("logicSwitch");

                        if ("a".equals(aSwitch)) {
                            out.collect("使用 1 号逻辑处理数据...");
                        } else if ("b".equals(aSwitch)) {
                            out.collect("使用 2 号逻辑处理数据...");
                        } else {
                            out.collect("使用 默认 号逻辑处理数据...");
                        }

                    }

                    // 处理广播流中的数据
                    @Override
                    public void processBroadcastElement(String value, Context ctx, Collector<String> out) throws Exception {
                        // 把广播流中的元素放入到广播状态中， 然后数据流中的每个并行度都可以获取到
                        BroadcastState<String, String> bcState = ctx.getBroadcastState(controlStateDesc);
                        bcState.put("logicSwitch", value);

                    }
                })
                .print();

        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
